<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 24, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web\Responses
{
	use KLF\KCore;
	use KLF\System\KComponent;
	
	/**
	 *  Class ResponsePage
	 *
	 *  Gets displayed when an error occurs.
	 *
	 *  @package KLF\System\Web\Responses
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @method KStandardResponse NewInstance
	 *  
	 *  @property int $ResponseCode
	 *  @property string $TemplateDirectory  
	 */
	class KStandardResponse extends KComponent
	{
		protected $_propModel;
		
		/**
		 * The HTTP Response Code
		 * 
		 * @var int
		 */
		protected $_rpropResponseCode;
		
		/**
		 * The template directory where the standard files are.
		 * 
		 * @var string
		 */
		protected $_propTemplateDirectory;
		
		/**
		 * Constructs a standard response
		 * 
		 * @param int $code
		 * @param KObject $dataModel
		 */
		public function __construct($code, $dataModel)
		{
			$this->_rpropResponseCode = $code;
			$this->_propTemplateDirectory = __DIR__;
			$this->_propModel = $dataModel;
		}
		
		/**
		 * Returns the template filename.
		 * 
		 * @return string
		 */
		public function GetTemplateFile()
		{
			return KCore::FS("{templateDirectory}{ds}Response{responseCode}.php", array(
				'templateDirectory'		=> $this->_propTemplateDirectory,
				'ds'					=> DIRECTORY_SEPARATOR,
				'responseCode'			=> $this->_rpropResponseCode));
		}
		
		/**
		 * 
		 * Enter description here ...
		 */
		public function RenderResponse()
		{
			//  Clean out anything remaining in the buffer.
			ob_clean();

			$model = $this->_propModel;
			include($this->GetTemplateFile());
			echo ob_get_clean();
		}
		
		public static function Render($code, $model)
		{
			$response = new KStandardResponse($code, $model);
			$response->RenderResponse();
		}
	}
}